package com.hilin.base.controller;

import com.hilin.base.server.feign.api.IUserApi;
import com.hilin.base.service.AccountService;
import com.hilin.common.common.BaseController;
import com.hilin.common.common.ReturnCode;
import com.hilin.common.util.AssertUtils;
import com.hilin.entity.anno.NoToken;
import com.hilin.entity.em.LoginOpType;
import com.hilin.entity.judge.AccountCreateJudge;
import com.hilin.entity.po.AccountInfoPo;
import com.hilin.entity.po.OrgInfoPo;
import com.hilin.entity.po.SysAccessPo;
import com.hilin.entity.po.SysRolePo;
import com.hilin.entity.vo.SignVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "用户接口")
@RestController
@RequestMapping("/api/account")
public class AccountController implements IUserApi {

    @Autowired
    private AccountService accountService;

    @ApiOperation(value = "创建或修改用户信息", notes = "不传ID视为创建，传入id则视为修改")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "认证字符串", name = "token", required = true, paramType = "header"),
            @ApiImplicitParam(value = "用户信息", name = "accountInfo", required = true, dataTypeClass = AccountInfoPo.class),
    })
    @PostMapping("/admin/create")
    public AccountInfoPo create(@RequestHeader("token") String token, @Validated(AccountCreateJudge.class) AccountInfoPo accountInfo) {
        accountInfo.setOrgId(accountInfo.getOrgId());
        return accountService.create(accountInfo);
    }

    @NoToken
    @ApiOperation(value = "登录")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "登录账号", name = "accountId", required = true),
            @ApiImplicitParam(value = "登录密码，需要进行md5加密", name = "password", required = true),
            @ApiImplicitParam(value = "机构ID，如果不填写，则可能会有多个相同登录账号（相同账号不同机构）", name = "orgId"),
            @ApiImplicitParam(value = "操作类型 none:不做处理 single:单设备登录(如果有账号未退出，则会提示已有账号登录)  force:强制登录(以前登录账号的token会强制失效)", name = "opType")
    })
    @PostMapping("/gen/login")
    public AccountInfoPo login(String accountId, String password, Long orgId, LoginOpType opType) {
        AssertUtils.isNotEmpty(accountId, ReturnCode.NORMAL_PARAM_LOST_ERROR.respMsg("账号不能为空"));
        AssertUtils.isNotEmpty(password, ReturnCode.NORMAL_PARAM_LOST_ERROR.respMsg("密码不能为空"));
        return accountService.login(accountId, password, orgId, opType);
    }

    @NoToken
    @ApiOperation(value = "后端服务获取token")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "机构ID", name = "orgId", required = true),
            @ApiImplicitParam(value = "随机字符串", name = "nonce", required = true),
            @ApiImplicitParam(value = "用户ID，这个字段为空则生成服务端token", name = "accountId"),
            @ApiImplicitParam(value = "时间戳，秒", name = "timestamp", required = true),
            @ApiImplicitParam(value = "签名字符串", name = "sign", required = true)
    })
    @GetMapping("/service/genToken")
    public String serviceGenToken(SignVo signVo) {
        AssertUtils.isNotEmpty(signVo, ReturnCode.NORMAL_PARAM_LOST_ERROR.resp());
        AssertUtils.isNotEmpty(signVo.getOrgId(), ReturnCode.NORMAL_PARAM_LOST_ERROR.respMsg("机构ID不能为空"));
        AssertUtils.isNotEmpty(signVo.getNonce(), ReturnCode.NORMAL_PARAM_LOST_ERROR.respMsg("随机字符串不能为空"));
        AssertUtils.isNotEmpty(signVo.getTimestamp(), ReturnCode.NORMAL_PARAM_LOST_ERROR.respMsg("时间戳不能为空"));
        AssertUtils.isNotEmpty(signVo.getSign(), ReturnCode.NORMAL_PARAM_LOST_ERROR.respMsg("签名字符串不能为空"));
        return accountService.serviceGenToken(signVo);
    }

    @NoToken
    @ApiOperation(value = "查询账号所属机构列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "登录账号", name = "accountId", required = true)
    })
    @GetMapping("/gen/accountOrgList")
    public List<OrgInfoPo> getAccountOrgList(@RequestParam String accountId) {
        AssertUtils.isNotEmpty(accountId, ReturnCode.NORMAL_PARAM_LOST_ERROR.respMsg("账号不能为空"));
        return accountService.getAccountOrgList(accountId);
    }

    @ApiOperation(value = "获取所有账号所有权限")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "认证字符串", name = "token", required = true, paramType = "header"),
    })
    @GetMapping("/gen/allAccess")
    @Override
    public List<SysAccessPo> getAllAccess(@RequestHeader String token) {
        return accountService.getAllAccess(BaseController.getSign());
    }

    @ApiOperation(value = "获取账号所有角色")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "认证字符串", name = "token", required = true, paramType = "header"),
    })
    @GetMapping("/gen/allRoles")
    @Override
    public List<SysRolePo> getAllRoles(@RequestHeader String token) {
        return accountService.getAllRoles(BaseController.getSign());
    }

    @ApiOperation(value = "获取角色权限列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "认证字符串", name = "token", required = true, paramType = "header"),
            @ApiImplicitParam(value = "角色ID", name = "roleId", required = true),
    })
    @GetMapping("/gen/role/access")
    @Override
    public List<SysAccessPo> getRoleAccess(@RequestHeader String token, @RequestParam Long roleId) {
        AssertUtils.isNotEmpty(roleId, ReturnCode.NORMAL_PARAM_LOST_ERROR.respMsg("角色ID不能为空"));
        return accountService.getRoleAccess(BaseController.getSign(), roleId);
    }

    @ApiOperation(value = "获取用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "认证字符串", name = "token", required = true, paramType = "header"),
            @ApiImplicitParam(value = "角色ID", name = "roleId", required = true),
    })
    @GetMapping("/gen/pageList")
    @Override
    public List<AccountInfoPo> pageList(@RequestHeader String token, AccountInfoPo accountInfo) {
        return accountService.pageList(accountInfo);
    }

}
